package com.example.suyuan.controller;

import com.example.suyuan.dao.TProductDao;
import com.example.suyuan.entity.JSONObject;
import com.example.suyuan.entity.TProduct;
import com.example.suyuan.entity.bo.ProductBo;
import com.example.suyuan.utils.DateUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.web3j.crypto.Credentials;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ProductController {

    @Resource
    private TProductDao tProductDao;

    @RequestMapping("/product/add")
    public ResponseEntity<JSONObject> add(@RequestBody ProductBo productBo){
        JSONObject jo = new JSONObject();

        if ("".equals(productBo.getProductName()) || productBo.getProductName() == null){
            jo.setCode("-1");
            jo.setMsg("产品名不能为空");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }
        if ("".equals(productBo.getProductDepa()) || productBo.getProductDepa() == null) {
            jo.setCode("-1");
            jo.setMsg("发货地不能为空");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }
        if ("".equals(productBo.getProductDest()) || productBo.getProductDest() == null){
            jo.setCode("-1");
            jo.setMsg("目的地不能为空");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }
        if ("".equals(productBo.getAddress()) || productBo.getAddress() == null){
            jo.setCode("-1");
            jo.setMsg("钱包地址不能为空");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }
        if ("".equals(productBo.getPrivateKey()) || productBo.getPrivateKey() == null){
            jo.setCode("-1");
            jo.setMsg("钱包私钥不能为空");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }


        // 从私钥创建 Credentials 对象
        Credentials credentials = Credentials.create(productBo.getPrivateKey());

        // 获取 Ethereum 地址
        String address = credentials.getAddress();
        if (!address.equals(productBo.getAddress())){
            jo.setCode("-1");
            jo.setMsg("私钥与地址不匹配，禁止操作");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }
        TProduct tProduct = new TProduct();
        tProduct.setProductName(productBo.getProductName());
        tProduct.setProductDepa(productBo.getProductDepa());
        tProduct.setProductDest(productBo.getProductDest());
        tProduct.setAddress(productBo.getAddress());
        tProduct.setCreateTime(DateUtils.getTime());
        tProductDao.save(tProduct);

        jo.setO("");
        jo.setMsg("新增产品成功");
        jo.setCode("1");
        return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
    }


    @RequestMapping("/queryProducts")
    public ResponseEntity<JSONObject> queryProducts(){
        JSONObject jo = new JSONObject();

        List<TProduct> product = tProductDao.queryAll();
        if (product.size() == 0){
            jo.setO("");
            jo.setMsg("暂无数据");
            jo.setCode("-1");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }

        jo.setO(product);
        jo.setMsg("查询成功");
        jo.setCode("1");
        return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
    }

    @RequestMapping(value = "/deleteProduct")
    public ResponseEntity<JSONObject> deleteProductByName(@RequestBody ProductBo productBo) {
        JSONObject jo = new JSONObject();

        // 从私钥创建 Credentials 对象
        if ("".equals(productBo.getPrivateKey()) || productBo.getPrivateKey() == null){
            jo.setCode("-1");
            jo.setMsg("钱包私钥不能为空");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }

        if ("".equals(productBo.getId()) || productBo.getId() == 0){
            jo.setCode("-1");
            jo.setMsg("id不能为空,不能为0");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }

        TProduct tProduct = tProductDao.queryById(productBo.getId());
        // 从私钥创建 Credentials 对象
        Credentials credentials = Credentials.create(productBo.getPrivateKey());

        // 获取 Ethereum 地址
        String address = credentials.getAddress();
        if (!address.equals(tProduct.getAddress())){
            jo.setCode("-1");
            jo.setMsg("私钥与地址不匹配，禁止操作");
            return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
        }

       tProductDao.deleteProduct(productBo.getId());

        jo.setCode("1");
        jo.setMsg("删除成功");
        return new ResponseEntity<JSONObject>(jo, HttpStatus.OK);
    }
}

